<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { loginApi } from '@/api/user'
import { useUserStore } from '@/stores/userStore'
import { useRouter } from 'vue-router'

const userStore = useUserStore()
const router = useRouter()

const loginForm = reactive({
  account: '',
  password: ''
})

const rules = {
  account: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 4, max: 10, message: '用户名长度应为4-10位', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9]+$/, message: '用户名只能包含英文和数字', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 8, max: 12, message: '密码长度应为8-12位', trigger: 'blur' },
    { pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,12}$/, message: '密码必须包含英文和数字', trigger: 'blur' }
  ]
}

const loginFormRef = ref(null)

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const res = await loginApi(loginForm)
        
        // 存储 token
        userStore.setToken(res.data)
        
        ElMessage.success('登录成功！')
        // 登录成功后跳转到主页面
        router.push('/')
      } catch (err) {
        ElMessage.error(err.message)
      }
    }
  })
}
</script>
    
<template>
  <div class="auth_container">
    <div class="auth_box">
      <h2>用户登录</h2>
      
      <!-- 登录表单 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="rules"
        label-width="80px"
        class="auth_form"
      >
        <el-form-item label="用户名" prop="account">
          <el-input v-model="loginForm.account" placeholder="请输入4-10位英文或数字用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入8-12位英文+数字密码"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" class="auth_btn">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
    
<style lang="less" scoped>
.auth_container {
  width: 100%;
  height: 100%;
  background-image: url(../assets/imgs/oonn.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;

  .auth_box {
    width: 500px;
    padding: 40px;
    background-color: rgba(255, 255, 255, 0.95);
    border-radius: 12px;
    box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.15);

    h2 {
      text-align: center;
      margin-bottom: 40px;
      color: #409EFF;
      font-size: 32px;
      font-weight: bold;
    }

    .auth_form {
      :deep(.el-form-item) {
        margin-bottom: 25px;
      }

      :deep(.el-form-item__label) {
        font-size: 18px;
        padding-bottom: 8px;
        line-height: 45px;
        height: 45px;
      }

      :deep(.el-form-item__content) {
        line-height: 45px;
      }

      :deep(.el-input__wrapper) {
        padding: 0 15px;
        height: 45px;
        box-sizing: border-box;
      }

      :deep(.el-input__inner) {
        font-size: 16px;
        height: 45px;
        line-height: 45px;
      }

      :deep(.el-form-item:last-child) {
        margin-bottom: 0;
        
        .el-form-item__content {
          margin-left: 0 !important;
        }
      }

      .auth_btn {
        width: 100%;
        height: 45px;
        font-size: 18px;
        margin-top: 20px;
      }
    }
  }
}
</style> 